package com.example.hdx.service;

import com.example.hdx.entity.Product;
import com.example.hdx.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public  class ProductService  {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 根据ID获取商品信息
     * @param productId 商品ID
     * @return 商品信息，如果不存在返回null
     */
    public Product getById(Long productId) {
        if (productId == null) {
            throw new IllegalArgumentException("商品ID不能为空");
        }

        // 使用MyBatis-Plus的selectById方法查询商品
        Product product = productMapper.selectById(productId);

        if (product == null) {
            throw new RuntimeException("商品不存在");
        }

        return product;
    }
}